package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/sum-of-matrix-after-queries/description/
 * @create 2025/10/13 19:01
 * @since JDK17
 */

public class Solution12 {
    public long matrixSumQueries(int n, int[][] queries) {
        int r = 0, c = 0;
        boolean[] row = new boolean[n], col = new boolean[n];
        long ans = 0;
        for (int i = queries.length - 1; i >= 0; i--) {
            var q = queries[i];
            if (q[0] == 0 && !row[q[1]]) {
                r++;
                row[q[1]] = true;
                ans += (n - c) * q[2];
            } else if (q[0] == 1 && !col[q[1]]){
                c++;
                col[q[1]] = true;
                ans += (n - r) * q[2];
            }
        }

        return ans;
    }
}
